<?

//Data loading
$title='KWITANSI';
$title2='RECEIPT';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';
$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm",array("239.4","138.6")); //array("239.4","138.6")
$pdf->AddPage();
$Result = GridList ();
$pdf->GridTable($Result);
$pdf->Output();


function GridList () {
	global $DBConnection, $DBName2;
		    //$DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
        $SQL  = "SELECT ais_receipt.*, $DBName2.vessel.nama, $DBName2.jadwalkapal.NOVOYAGE,	if (ifnull(tblreceipt.length,0)=0,'',tblreceipt.length) as size FROM ais_receipt ";
		$SQL .= "LEFT JOIN tblreceipt ON ais_receipt.thbl=tblreceipt.kdreceipt ";
        $SQL .= "LEFT JOIN $DBName2.vessel ON $DBName2.vessel.kodevessel = ais_receipt.vessel ";
        $SQL .= "LEFT JOIN $DBName2.jadwalkapal ON $DBName2.jadwalkapal.NOVOYAGE = ais_receipt.voyage ";
        $SQL .= "WHERE nokwit='".GetParam("nokwit","")."' ORDER BY urut ";
        return $DBConnection->dbc->get_results($SQL,ARRAY_A);
} 

function ReadHeader($nokwit) {
  global $DBConnection;
  $SQL = "	SELECT *,DATE_FORMAT(tglkwit,'%d %M, %Y') as spell_date, SUM(jumlah)  as TOTALPAID,
			if (ifnull(tbldebtur.CUNPWP,'')='','-',tbldebtur.CUNPWP) as CUNPWP
			FROM ais_receipt 
			LEFT JOIN tbldebtur ON ais_receipt.debtur=tbldebtur.CUCODE

			WHERE nokwit='$nokwit' 
			GROUP BY nokwit ";
  return $DBConnection->dbc->get_row($SQL);
}

class PDFReport extends PDF {

	function Header() {
		global $title,$title2,$perush,$page,$DEFINE_FONT,$DBConnection;
		if ($DEFINE_FONT==0) {
			$this->AddFont('Arial','','arialn.php');
			$DEFINE_FONT=1;
		}
	$Field = ReadHeader(GetParam("nokwit",""));
    $this->spell_date = strtoupper($Field->spell_date);
    list($this->year,$this->month,$this->day) = explode("-",$Field->tglbkt);

	$this->SetFont('Arial','',10);
    $this->SetLeftMargin(5);
//    $this->Cell(200,5,"",0,1,'L',0);
//    $this->Ln();$this->Ln();$this->Ln();
    $this->Cell(50,5,$perush,0,0,'L',0);
    $this->SetFont('Arial','',16);
    $this->Cell(105,5,$title." / ".$title2,0,0,'C',0);
	$this->SetFont('Arial','',10);
    $this->Cell(50,5,"NO: ".GetParam("nokwit",""),0,1,'L',0);
    $this->Cell(155,5,"",0,0,'C',0);
	$this->Ln();
//    $this->Cell(50,5,"NO ORDER: ",0,1,'L',0);
//	$this->Ln();
    $this->SetLeftMargin(5);
    //$this->Line(5,30,210,30);
	$this->SetFont('Arial','',8);
    $this->Cell(60,5,"SUDAH TERIMA DARI / RECEIVED FROM","LT",0,'L',0);
	$this->Cell(3,5,":","T",0,'L',0);
    $this->Cell(80,5,$Field->debnam,"TR",0,'L',0);
    $this->Cell(80,5,"ADDRESS :","TR",0,'L',0);
    $this->Ln();
    $this->Cell(60,5,"NPWP","L",0,'L',0);
	$this->Cell(3,5,":","",0,'L',0);
    $this->Cell(80,5,$Field->CUNPWP,"R",0,'L',0);
//    $this->Cell(62,5,"","R",0,'L',0);
	$this->MultiCell(80,5,$Field->CUADDR,"LR","");
//    $this->Ln();
    $this->Cell(60,5,"OPERATOR CONTAINER :","LB",0,'L',0);
	$this->Cell(3,5,":","",0,'L',0);
    $this->Cell(80,5,$Field->principal,"BR",0,'L',0);
    $this->Cell(80,5,"","BR",0,'L',0);

//    $this->Ln();
    $this->Ln();

    $cc = new intToRupiah;
    $GetCurrency = $DBConnection->dbc->get_var("SELECT DISTINCT tunam FROM tblcurrency, ais_receipt WHERE ais_receipt.nokwit='".GetParam("nokwit","")."' AND tblcurrency.tusymbol=ais_receipt.curr AND ktrasl='".GetOfficeID()."'");
    //$DBConnection->dbc->debug();
    $cc->setTail($GetCurrency);
    $cc->setNumber($Field->TOTALPAID);
    $SpellNumber = ucfirst(strtoupper(trim($cc->currency)));

    $this->Cell(223,5,"BANYAKNYA UANG ","LTR",0,'L',0);
    $this->Ln();
    $this->Cell(223,2,"-------------------------------- :  ".$SpellNumber,"LR",0,'L',0);
    $this->Ln();
    $this->Cell(223,5,"THE SUM OF ","LBR",0,'L',0);
    $this->Ln();
    $this->Ln();
    //Header
    //Column widths

    $w=array(15,83,30,15,15,35,30);
    $this->SetFont('Arial','',8);
    $this->Cell($w[0],5,"NO.",1,0,'C',0);        
    $this->Cell($w[1],5,"URAIAN / PEMBAYARAN",1,0,'C',0);
	$this->Cell($w[2],5,"LATTER NO",1,0,'C',0);
    $this->Cell($w[3],5,"QTY",1,0,'C',0);
	$this->Cell($w[4],5,"SIZE",1,0,'C',0);
	$this->Cell($w[5],5,"PRICE",1,0,'C',0);
    $this->Cell($w[6],5,"JUMLAH",1,0,'C',0);
    $this->Ln(); 
	}

	function GridTable($MyResult) {
	    //Column widths
            global $DBConnection;
    $w=array(15,83,30,15,15,35,30);
	    //Data
        if ($MyResult)
	    foreach($MyResult as $row)
	    {
			$Total = $Total + $row["jumlah"];
	        $this->Cell($w[0],5,$row["urut"].".",'LR',0,'R');
	        $this->Cell($w[1],5,$row["uraian"],'LR',0,'L');
	        $this->Cell($w[2],5,$row["container_no"],'LR',0,'L');
			$this->Cell($w[3],5,$row["jenis"],'LR',0,'R');
			$this->Cell($w[4],5,$row["size"],'LR',0,'C');
			$this->Cell($w[5],5,$row["curr"]." ".FormatNumber($row["dol"],2),'LR',0,'R');
			$this->Cell($w[6],5,$row["curr"]." ".FormatNumber($row["jumlah"]),'LR',0,'R');
	        $this->Ln();
	    }
                $cc = new intToRupiah;
                $GetCurrency = $DBConnection->dbc->get_var("SELECT DISTINCT tunam FROM tblcurrency, ais_advance WHERE ais_advance.nobkt='".GetParam("nobkt","")."' AND tblcurrency.tusymbol=ais_advance.curr AND ktrasl='".GetOfficeID()."'");
                //$DBConnection->dbc->debug();
                $cc->setTail($GetCurrency);
                $cc->setNumber($Total);
                $SpellNumber = ucfirst(strtoupper(trim($cc->currency)));

		        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5],5,"TOTAL:",1,0,'R',0);
		        $this->Cell($w[6],5,$row["curr"]." ".FormatNumber($Total,2),1,0,'R',0);
		        $this->Ln();
                $this->Ln();

                $this->Cell(15,5,"REMARK : ",0,0,'L',0);$this->Cell(50,5,$row["remark1"],0,0,'L',0);
                $this->Cell(110,5,"",0,0,'L',0);                
                $this->Cell(50,5,GetOfficeLocation().", ".$this->spell_date,0,1,'C',0);

                $this->Cell(15,5,"",0,0,'L',0);$this->Cell(50,5,$row["remark2"],0,1,'L',0);
                $this->Cell(15,5,"",0,0,'L',0);$this->Cell(50,5,$row["remark3"],0,1,'L',0);
                $this->Ln();
		      			$SQL = "SELECT * FROM $DBName.tblprofile WHERE ktrasl='".GetOfficeID()."'";
		            $Result = $DBConnection->dbc->get_row($SQL);
                
                $this->Cell(175,5,"",0,0,'L',0);$this->Cell(50,5,"( ".$Result->ttdkw." )",0,1,'C',0);

//	        			$this->Ln();
//                $this->Ln();

	    //Closure line
	}

	function Footer() {
	    //Closure line
	    //$this->Cell(205,0,'','T');
	}

}


?>
